package cn.nb12.xbox.controller;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import cn.nb12.xbox.po.Area;
import cn.nb12.xbox.po.Games;
import cn.nb12.xbox.po.Price;
import cn.nb12.xbox.respo.PriceGetInfo;
import cn.nb12.xbox.service.AreaService;
import cn.nb12.xbox.service.GamesService;
import cn.nb12.xbox.service.PriceService;
import cn.nb12.xbox.utils.Assist;
import cn.nb12.xbox.utils.GetGame;
import cn.nb12.xbox.utils.NumUtil;

@Controller
public class PriceController {
	
	
	@Autowired
	private PriceService priceService;
	
	@Autowired
	private GamesService gamesService;
	
	@Autowired
	private AreaService areaService;
	
	/**
	 * 查询一个游戏的详情
	 * @param view
	 * @param code
	 * @return
	 */
	@RequestMapping("price_goInfo")
	public ModelAndView goInfo(ModelAndView view,String code) {
		Assist assist=new Assist();
		assist.setRequires(Assist.andEq("code", code));
		List<Games> games = gamesService.selectGames(assist);
		if(games!=null && games.size()>0) {
			//根据id查游戏
			Assist assist2=new Assist();
			assist2.setRequires(Assist.andEq("GAM_ID", games.get(0).getId()));
			assist2.setOrder(Assist.order("money", true));
			List<Price> prices = priceService.selectPrice(assist2);
			List<PriceGetInfo> result=new ArrayList<>();
			if(prices!=null && prices.size()>0) {
				for (Price price : prices) {
					Area area = areaService.selectAreaById(price.getAreId());
					if(area!=null) {
						result.add(new PriceGetInfo(price.getGameName(), area.getCurrency(), price.getMoney(), NumUtil.format(price.getMoney()*area.getRate()),GetGame.getGameUrls(games.get(0).getCode(), area.getCode())));
					}
				}
			}
			view.getModel().put("data", result);
		}
		view.setViewName("pages/price/price_goInfo");
		return view;
	}
}
